package com.afkt.project.ui.adapter;

import com.afkt.project.model.bean.CommodityEvaluateBean;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.base.widget.BaseImageView;
import com.dev.base.widget.BaseTextView;
import com.dev.glide.utils.OhosGlide;
import com.dev.utils.common.BigDecimalUtils;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

public class SliderAdapter extends BaseItemProvider {

    private static final String TAG = "SliderAdapter";
    private List<CommodityEvaluateBean> data;
    private Context mContext;

    private int oldX;
    private int oldY;

    private int moveX;
    private int moveY;

    public SliderAdapter(List<CommodityEvaluateBean> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
    }

    public SliderAdapter(List<CommodityEvaluateBean> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_item_select, null, false);
            viewHolder = new ViewHolder();
            viewHolder.commodityName = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_ams_name_tv);
            viewHolder.commodityPrice = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_ams_price_tv);
            viewHolder.commodityImage = (BaseImageView) component.findComponentById(ResourceTable.Id_vid_ams_pic_igview);
            component.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }

        // ===========
        // = 商品信息 =
        // ===========

        // 商品名
        viewHolder.commodityName.setText(data.get(i).commodityName);
        // 商品价格
        viewHolder.commodityPrice.setText("￥" + BigDecimalUtils.operation(data.get(i).commodityPrice)
                .round(2, BigDecimal.ROUND_HALF_UP));
        // 商品图片
        try {
            OhosGlide.with(DevUtils.getAbilitySlice()).load(data.get(i).commodityPicture).def(ResourceTable.Media_icon).into(viewHolder.commodityImage);
        } catch (IOException e) {
            e.printStackTrace();
        }

        component.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        LogUtils.error(TAG, "按下");
                        oldX = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getX();
                        oldY = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getY();
                        break;
                    case TouchEvent.POINT_MOVE:
                        LogUtils.error(TAG, "移动");
                        moveX = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getX();
                        moveY = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getY();
                        int distanceX = moveX - oldX;
                        int distanceY = moveY - oldY;
                        LogUtils.error(TAG, "distanceX = "+distanceX);
                        if (Math.abs(distanceY) < 10) {
                            //表示左滑右滑
                            ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig();
                            if (distanceX < 0) {
                                //表示左滑
                                layoutConfig.setMarginLeft(distanceX);
                                component.setLayoutConfig(layoutConfig);
                            }else {
                                //表示右滑
                                layoutConfig.setMarginLeft(distanceX);
                                component.setLayoutConfig(layoutConfig);
                            }
                        }
                        break;
                    case TouchEvent.PRIMARY_POINT_UP:
                        LogUtils.error(TAG, "抬起");
                        break;
                }
                return false;
            }
        });
        return component;
    }


    static class ViewHolder {
        BaseImageView commodityImage;
        BaseTextView commodityName;
        BaseTextView commodityPrice;
    }
}
